package com.hb.service_edu;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;
import com.hb.guli.service.base.model.BaseEntity;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author jack
 * @date 2023/6/29 21:06
 * @description: MP 代码生成器
 */
public class CodeGenerator {

    // 快速
    @Test
    public void testGenerateCode(){
        String moduleName = "edu";
        FastAutoGenerator.create("jdbc:mysql://localhost:3306/guli_" + moduleName, "root", "123456")
                .globalConfig(builder -> {
                    builder.author("jack") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .dateType(DateType.ONLY_DATE)
                            .outputDir("D:\\project\\sgg\\java\\guli_parent\\service\\service_edu\\src\\main\\java"); // 指定输出目录
                })
                // 数据库配置
                .dataSourceConfig(builder ->
                        builder.dbQuery(new MySqlQuery())
                                .schema("guli_" + moduleName)
                                .typeConvert(new MySqlTypeConvert())
                                .keyWordsHandler(new MySqlKeyWordsHandler())
                )
                // 包配置
                .packageConfig(builder -> {
                    builder.parent("com.hb.guli.service") // 设置父包名
                            .moduleName(moduleName)
                            .entity("entity")
                            .service("service")
                            .mapper("mapper")
                            .xml("mapper.xml")
                            .controller("controller")
                            .pathInfo(Collections.singletonMap(OutputFile.xml,
                                    "D:\\project\\sgg\\java\\guli_parent\\service\\service_edu\\src\\main\\resources\\com\\hb\\guli\\service\\" + moduleName)); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder
//                            .addInclude("edu_teacher") // 设置需要生成的表名
                            .addTablePrefix(moduleName + "_"); // 设置过滤表前缀
                    builder.entityBuilder()
                            .enableChainModel()
                            .enableRemoveIsPrefix()  // 开启 Boolean 类型字段移除 is 前缀
                            .naming(NamingStrategy.underline_to_camel)
                            .columnNaming(NamingStrategy.underline_to_camel)
                            .idType(IdType.ASSIGN_ID)   // 主键策略
                            .logicDeleteColumnName("is_deleted")
                            .addTableFills(new Column("gmt_create", FieldFill.INSERT))
                            .addTableFills(new Property("gmtModified", FieldFill.INSERT_UPDATE))
                            .superClass(BaseEntity.class)
                            .addSuperEntityColumns("id","gmtCreate","gmtModified")
                            .enableLombok();
                    builder.serviceBuilder()
                            .formatServiceFileName("%sService");
                    builder.controllerBuilder()
                            .enableRestStyle()
                            .enableHyphenStyle();
                })
//                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .templateEngine(new VelocityTemplateEngine())
                .execute();
    }

    // 交互
    @Test
    public void testCodeGeneration()  {
        FastAutoGenerator.create(new DataSourceConfig.Builder("jdbc:mysql://127.0.0.1:3306/mybatis-plus", "root", "123456"))
                // 全局配置
//                .globalConfig((scanner, builder) -> builder.author(scanner.apply("请输入作者名称？")).fileOverride())
                // 包配置
                .packageConfig((scanner, builder) -> builder.parent(scanner.apply("请输入包名？")))
                // 策略配置
                .strategyConfig((scanner, builder) -> builder.addInclude(getTables(scanner.apply("请输入表名，多个英文逗号分隔？所有输入 all")))
                        .controllerBuilder().enableRestStyle().enableHyphenStyle()
                        .entityBuilder().enableLombok().addTableFills(
                                new Column("create_time", FieldFill.INSERT)
                        ).build())
                /*
                    模板引擎配置，默认 Velocity 可选模板引擎 Beetl 或 Freemarker
                   .templateEngine(new BeetlTemplateEngine())
                   .templateEngine(new FreemarkerTemplateEngine())
                 */
                .execute();
    }

    // 处理 all 情况
    protected static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }

}
